//
//  DataBaseTool.m
//  CateyeMovie
//
//  Created by dllo on 16/4/29.
//  Copyright © 2016年 anguoli. All rights reserved.
//

#import "DataBaseTool.h"

@implementation DataBaseTool


+ (DataBaseTool *)shareDataBase{
    static DataBaseTool *tool;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        tool = [[DataBaseTool alloc]init];
        [tool openDB];
        [tool createTable];
    });
    return tool;
}
- (void)openDB{
    NSString *sandBox = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES) lastObject];
//    NSString *docPath = [sandBox stringByAppendingString:@"Douban.sqlite"];
    NSString *docPath = [sandBox stringByAppendingPathComponent:@"Douban.sqlite"];

    NSLog(@"docPath: %@", docPath);

    int result = sqlite3_open([docPath UTF8String], &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
        
    } else {
        NSLog(@"失败%d", result);
    }
}
- (void)createTable{
    NSString *sql = @"create table if not exists movie (scid text primary key , ft text)";
    int result = sqlite3_exec(dbPoint, [sql UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"创建成功");
    } else {
        NSLog(@"创建表失败%d", result);
    }
}
- (SelectDB)isExistInTable:(NSString *)scid{
    NSString *sql = [NSString stringWithFormat:@"select * from movie where scid ='%@'", scid];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, [sql UTF8String], -1, &stmt, nil);
    if (result == SQLITE_OK) {
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            sqlite3_finalize(stmt);
            return SelecInTable;
        } else {
            sqlite3_finalize(stmt);
            return SelectNotInTable;
        }
        
    } else {
        
        sqlite3_finalize(stmt);//注销掉跟随指针
        NSLog(@"失败%d", result);
        return SelectError;
    }
}

- (BOOL)insertInTable:(AGLVideo *)movie{
    NSString *sql = [NSString stringWithFormat:@"insert into movie(scid, ft) values('%@', '%@')",movie.scid, movie.ft];
    
    int result = sqlite3_exec(dbPoint, [sql UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"添加成功");
        return YES;
    } else {
        NSLog(@"添加失败%d", result);
        return NO;
    }
}
- (BOOL)deleteFromTable:(NSString *)scid{
    NSString *sql = [NSString stringWithFormat:@"delete from movie where scid ='%@'", scid];
    int result = sqlite3_exec(dbPoint, [sql UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
        return  YES;
    } else {
        NSLog(@"删除失败%d", result);
        return NO;
        
    }
}


// 查询
//- (NSMutableArray *) moreModelIsExistInEditorRecommendTable {
- (NSMutableArray *) allModelIsExistInMovieTable {

    NSString *sqlStr = [NSString stringWithFormat:@"select * from movie"];
    sqlite3_stmt *stmt = nil;
    sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    
    NSMutableArray *array  = [NSMutableArray array];
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        int columnCount= sqlite3_column_count(stmt);
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        
        for (int j = 0; j < columnCount; j++) {
            const char *name= sqlite3_column_name(stmt, j);
            
            const unsigned char *value= sqlite3_column_text(stmt, j);
            
            [dic setValue:[NSString stringWithUTF8String:(const char *)value] forKey:[NSString stringWithUTF8String:name]];
            
        }
        [array addObject:dic];
    }
    
    sqlite3_finalize(stmt);
    return array;
}


@end
